import {Component, ViewChild} from '@angular/core';
import {Content, Events, IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpService} from "../../providers/HttpService";
import {InfoProvider} from "../../providers/InfoProvider";
import {CommProvider} from "../../providers/CommProvider";

/**
 * Generated class for the VipTradePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-vip-trade',
  templateUrl: 'vip-trade.html',
})
export class VipTradePage {

    @ViewChild(Content) content: Content;
    trade_radio: Array<any> = [
        {
            title: '全部',
            subTitle: '',
            value: 0,
            checked: true
        },
        {
            title: '买入',
            subTitle: '买入',
            value: 1,
            checked: false
        },
        {
            title: '卖出',
            subTitle: '卖出',
            value: 2,
            checked: false
        },
        {
            title: '平仓',
            subTitle: '平仓',
            value: 3,
            checked: false
        },
    ];

    trade_content: Array<string> = [
        '全部价位: ',
        '买入价位: ',
        '卖出价位: ',
        '平仓价位: '
    ];

    avatar_array: Array<string> = [
        'assets/icon/man1.png',
        'assets/icon/man2.png',
        'assets/icon/woman2.png',
    ];

    trade: string = '0';
    trade_type: number = 0;
    info_list: Array<any> = [];
    info_count: number = 0;
    start: number = 0;
    user_info: any;

    pub_trade: any = {
        type: 0,
        price: null,
        content: null,
        user_id: null,
        user_code: null
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private events: Events,
                private infoProvider: InfoProvider,
                private commProvider: CommProvider,
                private httpService: HttpService) {
        this.user_info = this.infoProvider.getUserInfo();

        if (this.infoProvider.isNative()) {
            this.events.subscribe('push:vip-trade', (data) => {
                console.log('push:vip-trade:', data);
                if (this.info_list.length > 0) {
                    this.info_list.unshift(data);
                    this.start = this.info_list.length;
                } else {
                    this.getVipTradeList(this.trade_type, 0, false).then(res => {
                        if (res.status == 0) {
                            this.info_list = res.data.rows;
                            this.start = this.info_list.length;
                        }
                    });
                }
            });
        }
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad VipTradePage');
        this.getVipTradeList(0, 0).then(res => {
            if (res.status == 0) {
                this.info_list = res.data.rows;
                this.start = this.info_list.length;
            }
        });
    }

    ionViewWillUnload() {
        this.events.unsubscribe('push:vip-trade', (data) => {
            console.log('ionViewWillUnload push:vip-trade:', data);
        });
    }

    getTradeInfoStyle(type: number) {
        return {
            'rising-price': type == 1,
            'falling-price': type == 2,
            'selling-price': type == 3,
        };
    }

    getPositionStyle(data: any) {
        return {
            'left': data.user_id != this.user_info.id,
            'right': data.user_id == this.user_info.id,
        };
    }

    getDateStyle(data: any) {
        return {
            'left-date': data.user_id != this.user_info.id,
            'right-date': data.user_id == this.user_info.id,
        };
    }

    getVipTradeList(type: number, start: number = 0, loader: boolean = true): Promise<any> {
        return new Promise((resolve, reject) => {
            this.httpService.instance()
                .setUrl('/vipTrade/getVipTrade')
                .setQuery({
                    type: type > 0 ? type : null,
                    start: start,
                    size: 10,
                    orderKey: 'id',
                    orderVal: 'desc',
                    today: true
                })
                .showLoader(loader)
                .getRequest()
                .then(res => {
                    this.info_count = res.data.count;
                    resolve(res);
                });
        });
    }

    onRefresh(refresher) {
        console.log('onRefresh:', this.trade_type);
        this.getVipTradeList(this.trade_type, 0, false).then(res => {
            if (res.status == 0) {
                this.info_list = res.data.rows;
                this.start = this.info_list.length;
            }
            refresher.complete();
        });
    }

    onInfinite(infiniteScroll) {
        setTimeout(() => {
            this.getVipTradeList(this.trade_type, this.start, false).then(res => {
                if (res.status == 0) {
                    this.info_list = this.info_list.concat(res.data.rows);
                    this.start = this.info_list.length;
                }
                infiniteScroll.complete();
            });
        }, 200);
    }

    onPubTradeInfo(tradeType: number) {
        let inputArray = [{
            name: 'price',
            placeholder: '请输入交易价位',
            type: 'number'
        }];
        this.commProvider.showInputAlert('发布交易价位', inputArray).then(resp => {
            console.log(resp);
            this.publicTrade(tradeType, resp.price);
        });
    }

    publicTrade(type: number, price: number) {
        this.pub_trade.type = type;
        this.pub_trade.user_id = this.user_info.id;
        this.pub_trade.user_code = this.user_info.code;
        this.pub_trade.user_phone = this.user_info.phone;
        this.pub_trade.user_name = this.user_info.name;
        this.pub_trade.user_gender = this.user_info.gender;
        this.pub_trade.price = price;
        console.log('onPubTrade:', this.pub_trade);

        if (!this.pub_trade.price) {
            this.commProvider.showToast('缺少交易价位');
            return;
        }

        this.pub_trade.content = this.trade_content[type] + this.pub_trade.price;
        this.commProvider.showConfirmAlert('发布交易信息', this.pub_trade.content)
            .then(confirm => {
                console.log('showConfirmAlert done');
                if (confirm) {
                    this.httpService.instance()
                        .setUrl('/vipTrade/addVipTrade')
                        .setBody(this.pub_trade)
                        .showLoader()
                        .postRequest()
                        .then(res => {
                            if (res.status == 0) {
                                this.commProvider.showToast('发布成功');
                            }
                        });
                }
            });
    }

    onShowMore() {
        this.commProvider.showRadioAlert('交易类型筛选', this.trade_radio).then(data => {
            console.log(data);
            this.trade_radio[this.trade_type].checked = false;
            this.trade_type = Number(data);
            this.trade_radio[this.trade_type].checked = true;

            this.getVipTradeList(this.trade_type, 0).then(res => {
                if (res.status == 0) {
                    this.info_list = res.data.rows;
                }
            });
        });
    }

    onScrollTop() {
        this.content.scrollToTop();
    }
}
